#!/bin/bash
prj_name="$1"
ftp_user="$2"
ftp_pwd="$3"
ftp_ip="$4"

xml_file_match=vendor/*/decouple-apps/delivery.xml
xml_file=`ls -1 $xml_file_match|head -1`
BASE_DIR=`pwd`
TMP_DIR=out/app.tmp
vendor_name=`echo "$xml_file"|awk -F"/" '{print $2}'`

if [ ! -f $xml_file ];then
   echo "It haven't found file[$xml_file]" >&2
   exit 0
fi

exit_func() {
   cd $BASE_DIR
   rm -fr $TMP_DIR
   echo "`basename $0` exit"
}

trap exit_func 0

begin_flag=0
version=""
project=""
apk_name=""
download_list=""
debug=0
while read line
do
   if echo "$line"|grep '<package-group' 1>/dev/null ;then
       begin_flag=1
       version=""
       project=""
       apk_name=""
       project=$(echo "$line"|awk -F 'name=\"' '{print $2}'|awk -F'"' '{print $1}')
       version=$(echo "$line"|grep -oP '[Vv]\d+\.\d+_\d+')
   fi

   if echo "$line"|grep '<package name' 1>/dev/null;then
       apk_name=$(echo "$line"|awk -F 'name=\"' '{print $2}'|awk -F'"' '{print $1}')
   fi

   last_flag=$begin_flag
   if echo "$line"|grep '</package-group' 1>/dev/null;then
      begin_flag=0
   fi

   if [ $last_flag -eq 1 ] && [ $begin_flag -eq 0 ];then
      [ $debug -eq 1 ] && echo "apk_name: $apk_name, project: $project, version: $version"
      [ $debug -eq 0 ] && printf "."
      if [ "$project"x = "$prj_name"x ];then
         if [ -z "$download_list" ];then
             download_list="$apk_name $version"
         else
             download_list=$(printf "%s\n%s %s" "$download_list" $apk_name $version)
         fi
      fi
   fi

done < $xml_file
echo ""
echo "parse $xml_file ...OK"

if [ $debug -eq 1 ];then
  echo "download_list: "
  echo "$download_list"
fi

ftp_down_sh=""
while read p v
do
         dst_dir="$TMP_DIR/$p"
         rm -fr $dst_dir
         debp=$(echo $p | tr '[:upper:]' '[:lower:]')
         debv=$(echo $v | cut -d '_' -f2)
         cmd="get APPS/$vendor_name/$p/$v/${debp}_${debv}.deb -o $dst_dir"
         [ ! -d $dst_dir ] && mkdir -p $dst_dir
         if [ -z "$download_list" ];then
             ftp_down_sh="$cmd"
         else
             ftp_down_sh=$(printf "%s\n%s" "$ftp_down_sh" "$cmd")
         fi
done <<EOF
$download_list
EOF

if [ $debug -eq 1 ];then
    echo "ftp_down_sh:"
    echo "$ftp_down_sh"
fi

echo "start downloading...."
lftp_flag=""
if [ $debug -eq 1 ];then
    lftp_flag="-d"
fi

#ftp_down_sh=`echo "$ftp_down_sh"|tail -1` #debug

if [ -z "$ftp_user" ] || [ -z "$ftp_pwd" ] || [ -z "$ftp_ip" ] ;then
   echo "Usage: `basename $` <prj_name> <ftp_user> <ftp_pwd> <ftp_ip>" >&2
   exit 0
fi

lftp $lftp_flag -u$ftp_user,$ftp_pwd $ftp_ip <<EOF
$ftp_down_sh
exit
EOF

ftp_down_sh=""
while read p v
do
    dst_dir="$TMP_DIR/$p"
    debp=$(echo $p | tr '[:upper:]' '[:lower:]')
    debv=$(echo $v | cut -d '_' -f2)
    echo "extract ${debp}_${debv}.deb ..."
    dpkg -x $dst_dir/${debp}_${debv}.deb .
    if [ $? -eq 0 ];then
        echo "extract ${debp}_${debv}.deb ...ok"
    else
        echo "extract ${debp}_${debv}.deb ...nok"
    fi
done <<EOF
$download_list
EOF

if [ $? -eq 0 ];then
   echo "...OK"
else
   echo "...NOK"
fi

